<?php

declare(strict_types=1);

namespace Modules\DataCheck\Models;

use Catch\Base\CatchModel as Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Modules\Permissions\Models\Traits\DataRange;
use Modules\User\Models\User;

/**
 * 数据核对任务目标数据下载日志模型
 * 
 * @property int $id 主键ID
 * @property string $task_id 任务id，dc_task表task_id
 * @property string $ip_address IP地址
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 软删除时间
 * @property int $create_user_id 创建人
 */
class DcTaskTargetDownloadLog extends Model
{
    use DataRange;
    
    /** 表名 */
    protected $table = 'dc_task_target_download_log';
    protected $primaryKey = 'id';
    public $timestamps = true;
    /** 允许填充字段 */
    protected $fillable = [
        'id',
        'task_id',
        'ip',
        'created_at',
        'updated_at',
        'deleted_at',
        'creator_id',
    ];

    /** 类型转换 */
    protected $casts = [
        'created_at' => 'integer',
        'updated_at' => 'integer',
        'deleted_at' => 'integer',
        'creator_id' => 'integer',
    ];

    /** 列表显示字段 */
    protected array $fields = [
        'id',
        'task_id',
        'ip',
        'created_at',
        'creator_id',
    ];

    /** 表单填充字段 */
    protected array $form = [
        'task_id',
        'ip',
        'created_at',
        'creator_id',
    ];
    /** 搜索字段 */
    public array $searchable = [
        'dc_task_target_download_log.task_id' => '=',
        'creator_id' => '=',
        'created_at' => 'between',
    ];
    
    /**
     * 关联任务表 - 一条下载日志属于一个任务
     */
    public function task(): BelongsTo
    {
        return $this->belongsTo(DcTask::class, 'task_id', 'task_id');
    }
    
    /**
     * 关联用户表 - 一条下载日志属于一个用户
     */
    public function downloadUser(): BelongsTo
    {
        return $this->belongsTo(User::class, 'creator_id', 'id');
    }
}